﻿namespace CardPlay.Model.GameModel.Actions
{
    public class MoveCardAction : CardAction
    {
        private readonly PartLocation _targetLocation;

        public MoveCardAction(CardGame cardGame, PartLocation from, PartLocation targetLocation)
            : base(cardGame, from)
        {
            _targetLocation = targetLocation;
        }
        
        protected override void OnExecute()
        {
            MoveCard(Location, _targetLocation);
        }

        protected override void OnReverse()
        {
            MoveCard(_targetLocation, Location);
        }
        
        private void MoveCard(PartLocation sourceLocation, PartLocation targetLocation)
        {
            var sourcePile = CardGame.PileAt(sourceLocation);
            var cardBeingMoved = sourcePile.CardAt(sourceLocation.Index);
            
            sourcePile.RemoveCardAt(sourceLocation.Index);
            
            CardGame.PileAt(targetLocation).InsertCard(targetLocation.Index, cardBeingMoved);
        }
        
    }
}
